package org.example.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.commonapi.domain.Practice;
import org.example.commonapi.dto.PracticeDto;
import org.example.commonapi.dto.PracticeInfoDto;
import org.example.practice.service.PracticeService;
import org.example.commonapi.mapper.PracticeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static org.example.commonapi.domain.Constants.VISIT_COUNT;


/**
* @author DELL
* @description 针对表【practice】的数据库操作Service实现
* @createDate 2024-05-31 21:26:16
*/
@Service
public class PracticeServiceImpl extends ServiceImpl<PracticeMapper, Practice>
    implements PracticeService {

    @Resource
    private PracticeMapper practiceMapper;

    /**
     * 查看公共题库
     * @param difficulty
     * @param type
     * @param name
     * @param subject
     * @return
     */
    @Override
    public List<PracticeInfoDto> list(Integer difficulty, Integer type, String name, String subject) {
        return practiceMapper.selectPublic(difficulty,type,name,subject);
    }

    /**
     * 查看我创建的题库
     * @param difficulty
     * @param type
     * @param name
     * @param subject
     * @param teacherId
     * @return
     */
    @Override
    public List<PracticeInfoDto> list(Integer difficulty, Integer type, String name, String subject, Long teacherId) {
        return practiceMapper.selectPrivate(difficulty,type,name,subject,teacherId);
    }


    /**
     * 添加题目
     * @param id
     * @param practice
     */
    @Override
    public Long insert(Long id, Practice practice) {
//        设置题目信息
        practice.setTeacherId(id);
        practice.setVisits(VISIT_COUNT);
        practice.setCreateTime(LocalDateTime.now());
        practice.setUpdateTime(LocalDateTime.now());
        practiceMapper.insert(practice);
        Long practiceId=practice.getId();
        return practiceId;
    }

    /**
     * 修改题目
     * @param practice
     */
    @Override
    public void update( Practice practice) {
//        设置信息
        practice.setUpdateTime(LocalDateTime.now());
        practiceMapper.updateById(practice);
    }

    /**
     * 单个删除题目
     * @param id
     */
    @Override
    public void delete(Long id) {
        practiceMapper.deleteById(id);
    }

    /**
     * 批量删除题目
     * @param ids
     */
    @Override
    public void deleteList(List<Long> ids) {
        practiceMapper.deleteBatchIds(ids);
    }


    /**
     * 查询单个题目
     * @param id
     * @return
     */
    @Override
    public Practice getOne(Long id) {
//        查询题目
        Practice practice=practiceMapper.getOne(id);
 //        设置更新条件
        practiceMapper.updateById(practice);
        practice.setVisits(practice.getVisits()+1);
        return practice;
    }


}




